#include <qpainter.h>
#include <cstdio>

#include "MapView.hpp"

const char *filename_format = "/tmp/gps_%d_G%d-%d.bmp";

extern int zoom_factor;
extern int img_idx[4][2];
extern float pos_x, pos_y;

MapView::MapView( QWidget* p )
  :QWidget(p)
{
}

MapView::~MapView( )
{
}

void MapView::paintEvent( QPaintEvent *event )
{
  QPainter painter( this );
  char fname[32];

  // 画场景
  int x = pos_x*width(), y = pos_y*height();
  int _x, _y;
  for( int i=0; i < 2; ++i )
    {

      for (int j=0; j < 2; ++j){
	int k = i*2+j;
	if(img_idx[k][0] >= 0 && img_idx[k][1] >= 0 ){
	  sprintf(fname, filename_format, zoom_factor, img_idx[k][0], img_idx[k][1]);

	  // 计算x,y
	  _x = x+j*width();
	  _y = y+(1-i)*height();

	  img.load( fname );
	  painter.drawImage( _x, _y, img );
	} // end of img_idx[][] >= 0
      }	// end of j

    } // end of i

  QPen pen( QColor(255, 0, 255) );

  painter.setPen( pen );
  painter.drawRect( width()/2-2, height()/2-2, 4, 4 );
}
